/**
 * 多选插件
 * @param $
 */(function($) {
	
	$.fn.morec = function(options) {
		var newInput,$_morec;
        var opts = $.extend({}, $.fn.morec.defaults, options);
        return this.each(function() {
            var $this = $(this);
            var nid = $this.attr("id")+"_morec";
            var nname=$this.attr("name")+"_morec";
            var clazz=$this.attr("class");
            $this.attr("type","hidden");
            newInput=$("<div  id='"+nid+"' name='"+nname+"' class=' morec_edit'>"+opts.content+"</div>"); 
            $this.after(newInput);
            var _morecDIV ='<div class="col-lg-12 morec" name="" >'+
            			'	<div class="col-lg-12 morecHeader">'+
						'  		<h4>请选择</h4>'+
						'  		<div class="btn-group" style="float:right;">'+
						'        	<button class="btn btn-warning morecCheckAll" type="button" class=""><i class="fa fa-check-square"></i>全选</button>'+
						'        	<button class="btn btn-primary morecToggleCheck" type="button"><i class="fa fa-check-square-o"></i>反选</button>'+
						'        	<button class="btn btn-info morecNoCheck" type="button"><i class="fa fa-plus"></i>全不选</button>'+
						'        	<button class="btn btn-default morecClose" type="button"><i class="fa fa-times"></i>取消</button>'+
						'  		</div>'+
						' 	</div>'+
						'  	<div class="col-lg-12 morecContent">'+
						'		<ul class="col-lg-12">'+
						'		</ul>'+
						'  	</div>'+
						'   <div class="col-lg-12 morecFooter" >'+
						'		<div class="btn-group" style="float:right;">'+
						'			<button class="btn btn-primary morecsub" type="button" ><i class="fa fa-check"></i>确定</button>'+
						'		 </div>'+
						'	</div>'+
						'</div>';
            $_morec=$(_morecDIV);
            $("body").append($_morec);
            $_morec.find(".morecCheckAll").on("click",function(){
            	$_morec.find("input[type='checkbox']").each(function(){
            			$(this).prop("checked",true);
            	})
            });
            $_morec.find(".morecToggleCheck").on("click",function(){
            	$_morec.find("input[type='checkbox']").each(function(){
            		if($(this).prop("checked"))
        				$(this).prop("checked",false);
        			else
        				$(this).prop("checked",true);
            	})
            });
            $_morec.find(".morecNoCheck").on("click",function(){
            	$_morec.find("input[type='checkbox']").each(function(){
            		$(this).removeProp("checked");
            	})
            });
            $_morec.find(".morecClose").on("click",function(event){
            	hide(event, newInput,$_morec);
			});
            $_morec.find(".morecsub").on("click",function(event){
				var ids="",names="";
				$_morec.find("input[type='checkbox']").each(function(){
            		if($(this).prop("checked"))
            		{
            			ids+=$(this).attr("morec-id")+",";
            			var temp="<span>"+$(this).attr("morec-data")+"</span>";
            			names+=temp;
            		}
            	})
            	$this.val(ids);
        		newInput.html(names);
        		hide(event, newInput,$_morec);
			});
			$.fn.morec.loadElement($_morec,opts);
			newInput.on("click",function(event){
				show(event,newInput,$_morec);
			});
			
			var show=function(){
	        	var obj = $(newInput);//pName是触发tree的元素的id 使用时候请自行修改
	            var objset = $(newInput).offset();
	            $_morec.css({
	                left : objset.left + "px",
	                top : objset.top + obj.outerHeight() + "px",
	            }).slideDown("fast");
	            $("body").bind("mousedown", onBodyDown);
	        };
	        var hide=function(){
	         $_morec.fadeOut("fast");
	       	 $("body").unbind("mousedown", this.onBodyDown);
	        };
	        var onBodyDown=function (event) {
	           if (!($(event.target).attr("name")==newInput.attr("name")||$(event.target).attr("name")==$_morec.attr("name")||$(event.target).parents(".morec").length > 0)) {
	               hide();
	           }
	       }
        });
        
    };
    
    $.fn.morec.loadElement = function(element, opts) {
        $.ajax({
            url : opts.ajaxurl,
            type : 'get',
            data : {
            	userid:opts.userid,
                page :opts.page,
                rows:opts.rows
            },
            datatype : "json",
            success : function(returned) {
            	var t="";
            	var data = returned;
            	var len = data.length;
            	if(len>0)
            	{
            		for(var i=0;i<len;i++)
            		{
            			t+="<li ><input type='checkbox'  morec-data='"+data[i].name+"' morec-id='"+data[i].id+"'>&nbsp;&nbsp;"+data[i].name+"</li>";
            		}
            	}
            	else
            		t="抱歉，暂无相关数据！";
            	$(element).find("ul").html("");
            	$(element).find("ul").html(t);
            },
            erorr : function(request, erorr, exp) {
                alert(erorr);
            }
        });
    };
    $.fn.morec.randomString=function(len) {
    	len = len || 32;
    	var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    	var maxPos = $chars.length;
    	var pwd = '';
    	for (i = 0; i < len; i++) {
    		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    	}
    	var timestap=new Date().getTime()/1000
    	pwd = pwd +timestap;
    	return pwd;
    };
    $.fn.morec.defaults ={
    	userid:"",
    	rows:100,
    	page:1,
    	pageCount:1,
    	content:"",
    	ajaxurl:_ctx+"/admin/security/permission/tree.json"
    }
})(jQuery);